/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.widget.ListView;

public class BarChartView extends ListView {

	private static final int STATE_BEFORE_ANIMATION = 0;
	private static final int STATE_ANIMATING = 1;
	private static final int STATE_AFTER_ANIMATION = 2;
	// the state of animation of bar chart.
	private int animationState = STATE_BEFORE_ANIMATION;

	public BarChartView(Context context) {
		super(context);
		//set empty view
//		LayoutInflater inflater = LayoutInflater.from(context);
//		View emptyView = inflater.inflate(R.layout.list_empty_view_bar_chart, null);
//		emptyView.setVisibility(View.GONE);
//		((ViewGroup)getParent()).addView(emptyView);
//		setEmptyView(emptyView);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (animationState == STATE_BEFORE_ANIMATION) {
			new Thread(new BarAnimationRunnable(), "Anim_Bar_Inc")
					.start();
		} else {
			super.onDraw(canvas);
		}
	}

	private class BarAnimationRunnable implements Runnable {

		@Override
		public void run() {
			animationState = STATE_ANIMATING;
			BarView.animationPercent = 0;
			while (BarView.animationPercent < 1) {
				BarView.increaseAnimationPercent();
				postInvalidate();
				try {
					Thread.sleep(30);
				} catch (InterruptedException e) {
				}
			}
			animationState = STATE_AFTER_ANIMATION;
		}
	}

}
